<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <meta property="qc:admins" content="63350727776371646636" />
  
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
  <title> SwiftV课堂 - 中国最大的Swift视频学习站</title>
  <meta name="keywords" content="swift,swift 教程,swift视频,swift论坛,swiftv课堂,swift培训,swift开发,ios8 开发,ios9 编程" />
  <meta name="description" content="SwiftV课堂是国内氛围最好的swift学习、交流网站，提供上千套swift视频,swift教程免费观看，学Swift开发(iOS10 开发)就上SwiftV课堂。" />
  <link rel="shortcut icon" type="image/x-icon" href="http://static.vlms.cn/images/2014/08/hyjpep4o.png" media="screen" />
  
  <link href="http://static.vlms.cn/resources/bootstrap/css/bootstrap.css" rel="stylesheet" />
  <link rel="stylesheet" media="screen" href="http://static.vlms.cn/resources/bootstrap/css/bootstrap-extends.css" />
  
  <!-- 预览、自定义、默认css样式  -->
  	<link rel="stylesheet" media="screen" href="http://static.vlms.cn/resources/css/schoolweb.css" />
  
  <!-- 回到顶部样式  -->
  <link rel="stylesheet" media="screen" href="http://static.vlms.cn/resources/css/upDown.css" />
  
  <script src="http://static.vlms.cn/resources/js/jquery.min.js"></script>
  <script src="http://static.vlms.cn/resources/js/main.js"></script>
  
  <!-- 回到顶部样式  -->
  <script src="http://static.vlms.cn/resources/js/JQscrollTop.js"></script>
  <script src="http://static.vlms.cn/resources/js/updown.js"></script>
  
    <!--[if lt IE 8]>
      <link href="//static.vlms.cn/resources/wangxiaoyun/css/oldie.css" rel="stylesheet">
    <![endif]-->
    
    <!--[if lt IE 9]>
    <script src="//static.vlms.cn/resources/wangxiaoyun/libs/html5shiv.js"></script>
    <![endif]-->

	  <!--[if IE 8]>
	    <script src="//static.vlms.cn/resources/wangxiaoyun/libs/respond.min.js"></script>
	  <![endif]-->
  
</head>
<body id="bodyColor" class="">

<style>
.nav-list > li.active > a {
    background-color: #ffa275;  
    color: #ffffff;
}

.site-navbar .navbar-nav > li > a:hover {
    background-color: #ffa275;
    color: #ffffff;
}
</style>

<div class="navbar navbar-inverse site-navbar" id="site-navbar">
  <div class="container">
    <div class="container-gap">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
          <a class="navbar-brand-logo" href="../index.html">
            <img src="http://static.vlms.cn/images/2015/07/ic4hqb7b.png" alt="SwiftV课堂" title="SwiftV课堂" height="55px;" />
          </a>
      </div>
      
      <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
              <li><a href="../course/explore-sort=latest.html"  >全部课程</a></li>
              <li><a href="https://www.boxueio.com/skill/swift?v"  target="_blank"  >如何学Swift？</a></li>
              <li><a href="../page/qun.html"  >Q群公布</a></li>
              <li><a href="../page/lianxi.html"  >联系我们</a></li>
          </ul>
        
        <ul class="nav navbar-nav navbar-right">
          <li>
	        <span class="nav navbar-nav" style="margin-top:10px;width:170px;">
	          <input class="typehead form-control search_input" type="text" placeholder="搜索课程或用户" style="display:inline;background:#5c5f68; border: 0px solid #5c5f68;width:130px;" data-role="item-input">
		      <button class="btn btn-default xin-search-btn" type="submit" data-role="item-add" style="margin-left:-10px;margin-top:-3px;background-color:#5c5f68;border: 1px solid #5c5f68;color:#a1a4ac;"><i class="glyphicon glyphicon-search"></i></button>
	    	</span>            
          </li>  
            <li><a href="../login.html">登录</a></li>
               <li><a href="../register.html">注册</a></li>
        </ul>        

      </div><!--/.navbar-collapse -->
    </div>
  </div>
</div>


<script>
   $(document).ready(function(){
      $(".typehead").click(function(){
          $(this).css("background","white");
          $(".xin-search-btn").css({"background":"white","border":"1px solid #FFFFFF"});
      });
      
   	  $(".typehead").blur(function(){
          $(this).css("background","#5c5f68");
           $(".xin-search-btn").css({"background":"#5c5f68","border":"1px solid #5c5f68"});
      });
      
	  //搜索按钮
	  $(".xin-search-btn").click(function(){
	   	var key=$(".search_input").val();
	   	var url = "";
	   	if (getUrlParam('theme')) {
	   		var value = getUrlParam('theme');
	   		url="/search/course?keyword="+key+"&theme="+value;
	   	}else{
			url="/search/course?keyword="+key;
		}
		location.href=url;
	  });
	  
	  	//获取url中的参数
		function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg);  	//匹配目标参数
			if (r != null) return unescape(r[2]); return null; 		//返回参数值
		}
	
   });
</script>


<div id="content-container" class="container">
        <div class="wxy-row-wrap container-gap" style="padding:15px 26px;">
  		<div class="row form-group">
  		
			    <div class="col-md-12">
			      <div class="page-header">
			        <h1>Swift 3 SE-0005 更好的把Objective-C APIs转换成Swift版本  <div class="pull-right" style="font-size:12px;padding:26px 20px; color:#7abaf5;">
			        <span>
		        		日期：2016-06-22 12:26:26
	        		</span>
	        		</div></h1>
			       
			      </div>
			      
			    </div>
	 
	  			<div class="col-md-12" style="padding-bottom:20px;">
			    <div class="control-label"> <label for="content">摘要：</label> </div>
			        <p style="padding-left:30px;padding-right:30px;"> 点评：大家都在讨论Apple new SDK，你也一定在各种地方看到过了，这个API被改成了什么什么样子的文章。究竟这个庞大的工作是如何执行下去的？还是认真研读下Apple官方的解读吧。
</br>
译文链接：<a href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md" target="_blank"><span style="color:#337FE5;"><u>https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md</u></span></a><span style="line-height:1.5;"></span></p>
			     </div>
			     
			    <div class="col-md-12" style="padding-bottom:20px;">
			    <div class="control-label"> <label for="content">内容：</label> </div>
			        <p style="padding-left:30px;padding-right:30px;"> <h2 style="font-size:1.75em;font-family:;">
	提交review前必读
</h2>
<p style="font-family:" font-size:16px;"="">
	做为下面三份文档的一部分，它们的内容是彼此关联的：
	</p>
<ul style="font-family:" font-size:16px;"="">
	<li>
		<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0023-api-guidelines.md">SE-0023 API设计指南</a> 
	</li>
	<li>
		<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0006-apply-api-guidelines-to-the-standard-library.md">SE-0006 在标准库中应用设计指南</a> 
	</li>
	<li>
		<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md">SE-0005 更好的把Objective-C APIs转换成Swift版本</a> 
	</li>
		</ul>
<p style="font-family:" font-size:16px;"="">
	这三份文档的内容是相互关联的（例如：标准库中一个API的调整和某个API guideline是对应的，或根据某条设计指南制定的Clang importer规则，等等）。正因为存在这些内容交叉，为了保证讨论是可维护的，我们希望你：
			</p>
<ul style="font-family:" font-size:16px;"="">
			<li>
				<span style="font-weight:bolder;">在提交review之前，对以上三份文档中的全部内容，有一个基本的了解</span>；
			</li>
			<li>
				<span style="font-weight:bolder;">在提交以上三个文档的review时，请参照每个文档的review声明</span>。在你提交review时，如果文档间交叉引用有助于帮你阐述观点，你应该包含它们（这也是被提倡的做法）。
			</li>
				</ul>
				<h2 style="font-size:1.75em;font-family:;">
					<a id="user-content-简介" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E7%AE%80%E4%BB%8B"></a>简介
				</h2>
<p style="font-family:" font-size:16px;"="">
	这份提议描述了我们如何改进Swift的“Clang Importer”，它有两个功能：首先，把C和Objective-C的APIs映射成Swift版本；其次，翻译Objective-C中的函数、类型、方法和属性等的名字，让它们满足<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0023-api-guidelines.md">API设计指南</a>中的要求，这些要求，是我们在设计Swift 3时，建立的原则。
					</p>
<p style="font-family:" font-size:16px;"="">
	我们的方法专注于Objective-C版本的<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html">Cocoa编码指南</a>和Swift版本的<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0023-api-guidelines.md">API设计指南</a>之间的差异。使用一些简单的语言学分析方法，协助我们把Objective-C中的名字自动转换成更加Swift“原汁原味”的名字。
				</p>
<p style="font-family:" font-size:16px;"="">
	转换的结果，可以在<a href="https://github.com/apple/swift-3-api-guidelines-review">Swift 3 API Guidelines Review</a>这个repository中查看。这个repository中包含了用<a href="https://github.com/apple/swift-3-api-guidelines-review/tree/swift-2">Swift 2</a>和<a href="https://github.com/apple/swift-3-api-guidelines-review/tree/swift-3">Swift 3</a>编写的Objective-C APIs项目，以及一些已经迁移到Swift 3版本的示例代码。你也可以通过<a href="https://github.com/apple/swift-3-api-guidelines-review/compare/swift-2...swift-3">对比这两个分支</a>来查看所有的改动。
					</p>
					<h2 style="font-size:1.75em;font-family:;">
						<a id="user-content-动机" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%8A%A8%E6%9C%BA"></a>动机
					</h2>
<p style="font-family:" font-size:16px;"="">
	Objective-C版本的<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html">Cocoa编码指南</a>为使用Objective-C创建简单、一致的API提供了完整的框架。但是Swift是一门不同的编程语言，特别是，它是一门支持类型推导、泛型编程和重载等语言特性的强类型语言。于是，基于Objective-C编写的APIs搭配上Swift就有点儿水土不服，这些API在Swift里用起来显得很啰嗦。例如：
				</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">let</span> content <span class="pl-k" style="color:#A71D5D;">=</span> listItemView<span class="pl-k" style="color:#A71D5D;">.</span>text<span class="pl-k" style="color:#A71D5D;">.</span>stringByTrimmingCharactersInSet(
        NSCharacterSet<span class="pl-k" style="color:#A71D5D;">.</span>whitespaceAndNewlineCharacterSet())</pre>
					</div>
<p style="font-family:" font-size:16px;"="">
	这明显是一个Objective-C风格的函数调用。如果我们用Swift编写，结果看上去应该是这样的：
						</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">let</span> content <span class="pl-k" style="color:#A71D5D;">=</span> listItemView<span class="pl-k" style="color:#A71D5D;">.</span>text<span class="pl-k" style="color:#A71D5D;">.</span>trimming(<span class="pl-k" style="color:#A71D5D;">.</span>whitespaceAndNewlines)</pre>
							</div>
<p style="font-family:" font-size:16px;"="">
	这显然是更遵循<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0023-api-guidelines.md">Swift API设计指南</a>中的用法，特别是，我们忽略掉了那些编译器已经能强制约束我们使用的类型名称（例如：view，string, character set等）。这份提议的目的，就是让从Objective-C引入API更加“Swift原汁原味”，让Swift开发者在使用Objective API时，有和使用Swift“原生代码”更为一致的开发体验。
								</p>
<p style="font-family:" font-size:16px;"="">
	这份提议中的解决方案对Objective-C框架（例如：Cocoa和Cocoa Touch）和任何可以在“Swift混合项目”中使用的Objective-C API是相同的。要说明的是，<a href="https://swift.org/core-libraries/">Swift核心库</a>重新实现了Objective-C框架中的API，所以，对这些API（名称的）的改动都会在Swift 3核心库的实现中体现出来。
							</p>
							<h2 style="font-size:1.75em;font-family:;">
								<a id="user-content-提议的解决方案" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E6%8F%90%E8%AE%AE%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"></a>提议的解决方案
							</h2>
<p style="font-family:" font-size:16px;"="">
	提议的解决方案引入了一种定义Objective-C&nbsp;<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html">Cocoa编码指南</a>和<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0023-api-guidelines.md">Swift API设计指南</a>区别的方式，这种方式可以帮助我们通过设置一系列规则，参照Cocoa编码指南以及Objective-C中既定的习俗，把前者变换成后者。这是对用Clang importer进行名称翻译的一种启发式扩展。例如：把Objective-C中的全局enum常量变成Swift中的cases（这要去掉Objective-C为全局enum常量名设置的前缀）以及把Objective-C中的工厂方法（例如：+[NSNumber numberWithBool:]）映射成Swift中的初始化方法（NSNumber(bool: true)）。
								</p>
<p style="font-family:" font-size:16px;"="">
	这份提议中描述的启发式方法需要通过覆盖大量的Objective-C API进行迭代、调校和试验，以确保它最终可以正常工作。但是，它仍旧是不可能完美工作的，一定会有一些API，经过“翻译”之后，会导致其不如原来表意清晰。因此，我们的目标是确保绝大多数的Objective C API都可以在翻译之后更加的Swift原汁原味。并且允许Objective-C API的作者对于那些不满意的翻译，在Objective-C头文件中，通过API注释说明问题。
							</p>
<p style="font-family:" font-size:16px;"="">
	这份提议的解决方案对Clang importer引入了以下这些改变：
								</p>
<ol style="font-family:" font-size:16px;"="">
								<li>
									<span style="font-weight:bolder;">泛化swift_name属性的应用范围</span>：Clang的swift_name现在只能用于重命名enum的cases以及工厂方法。当它被引入到Swift后，它应该被泛化成允许重命名任意的C或Objective-C的语言元素，以方便C或Objective-C API的作者更好的调校重命名的过程。
								</li>
								<li>
									<span style="font-weight:bolder;">去掉冗余的类型名称</span>：Objective-C Cocoa编码指南要求方法声明中要带有每一个参数的描述。当这个描述重申了参数的类型时，方法的名字就违背了Swift编码指南中关于“忽略不需要的字符”的设计要求。因此，执行翻译时，我们应该去掉那些描述类型的部分。
								</li>
								<li>
									<span style="font-weight:bolder;">添加默认参数</span>：如果Objective-C API的声明强烈暗示参数需要参数默认值，应该为这样的API在引入Swift时，添加参数默认值。例如，一个表示选项集合的参数，可以被设置成[]。
								</li>
								<li>
									<span style="font-weight:bolder;">为第一个参数添加label</span>：如果方法的第一个参数有默认值，<a href="https://swift.org/documentation/api-design-guidelines#first-argument-label">应该为这个参数设置一个参数label</a>。
								</li>
								<li>
									<span style="font-weight:bolder;">给Bool语义的属性添加“is”前缀</span>：<a href="https://swift.org/documentation/api-design-guidelines#boolean-assertions">Bool属性应该在读取的时候，表达断言的语义</a>，但是Objective-C Cocoa编码指南中，<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#/apple_ref/doc/uid/20001284-BAJGIIJE">禁止在属性名中使用单词“is”</a>。因此，当引入这样的属性时，为它们添加“is”前缀。
								</li>
								<li>
									<span style="font-weight:bolder;">表达值语义的名字，首字母小写</span>：在Swift API设计指南中，要求对“非类型声明（non-type declarations）”使用小写字母。包括，enum中的case以及属性或函数的声明。因此，引入Objective-C中没有前缀的值时，让这些名字的首字母小写（例如：一个叫做URLHandler的属性应该变成urlHandler）。
								</li>
								<li>
									<span style="font-weight:bolder;">让实现compare(_:) -&gt; NSComparisonResult的类遵从Comparable protocol</span>：在Objective-C中，类对象的比较结果，都是通过“排序”的方式判断的（注：例如NSOrderedDescending和NSOrderedAscending）。导入过程中，通过让这些类遵从Comparable&nbsp;protocol，可以让比较操作的实现更正规（注：通过Comparable提供的运算符方法）。
								</li>
									</ol>
<p style="font-family:" font-size:16px;"="">
	为了感受下这些转换规则带来的实际效果，看下UIBezierPath从Swift 2：
										</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">class</span> UIBezierPath <span class="pl-k" style="color:#A71D5D;">:</span> NSObject, NSCopying, NSCoding { <span class="pl-k" style="color:#A71D5D;">convenience</span> <span class="pl-k" style="color:#A71D5D;">init</span>(ovalInRect: CGRect) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">moveToPoint</span>(_: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addLineToPoint</span>(_: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addCurveToPoint</span>(_: CGPoint, 
        controlPoint1: CGPoint, controlPoint2: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addQuadCurveToPoint</span>(_: CGPoint, 
        controlPoint: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">appendPath</span>(_: UIBezierPath) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">bezierPathByReversingPath</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIBezierPath <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">applyTransform</span>(_: CGAffineTransform) <span class="pl-k" style="color:#A71D5D;">var</span> empty: <span class="pl-c1" style="color:#0086B3;">Bool</span> { <span class="pl-k" style="color:#A71D5D;">get</span> } <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">containsPoint</span>(_: CGPoint) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Bool</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fillWithBlendMode</span>(_: CGBlendMode, alpha: CGFloat) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">strokeWithBlendMode</span>(_: CGBlendMode, alpha: CGFloat) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">copyWithZone</span>(_: NSZone) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">AnyObject</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">encodeWithCoder</span>(_: NSCoder)
}</pre>
											</div>
<p style="font-family:" font-size:16px;"="">
	移植到Swift 3后的变化：
												</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">class</span> UIBezierPath <span class="pl-k" style="color:#A71D5D;">:</span> NSObject, NSCopying, NSCoding { <span class="pl-k" style="color:#A71D5D;">convenience</span> <span class="pl-k" style="color:#A71D5D;">init</span>(ovalIn rect: CGRect) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">move</span>(<span class="pl-en" style="color:#795DA3;">to</span> <span class="pl-smi">point</span>: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addLine</span>(<span class="pl-en" style="color:#795DA3;">to</span> <span class="pl-smi">point</span>: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addCurve</span>(<span class="pl-en" style="color:#795DA3;">to</span> <span class="pl-smi">endPoint</span>: CGPoint, <span class="pl-en" style="color:#795DA3;">controlPoint1</span> <span class="pl-smi">controlPoint1</span>: CGPoint, <span class="pl-en" style="color:#795DA3;">controlPoint2</span> <span class="pl-smi">controlPoint2</span>: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addQuadCurve</span>(<span class="pl-en" style="color:#795DA3;">to</span> <span class="pl-smi">endPoint</span>: CGPoint, <span class="pl-en" style="color:#795DA3;">controlPoint</span> <span class="pl-smi">controlPoint</span>: CGPoint) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">append</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">bezierPath</span>: UIBezierPath) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">reversing</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIBezierPath <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">apply</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">transform</span>: CGAffineTransform) <span class="pl-k" style="color:#A71D5D;">var</span> isEmpty: <span class="pl-c1" style="color:#0086B3;">Bool</span> { <span class="pl-k" style="color:#A71D5D;">get</span> } <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">contains</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">point</span>: CGPoint) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Bool</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fill</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">blendMode</span>: CGBlendMode, <span class="pl-en" style="color:#795DA3;">alpha</span> <span class="pl-smi">alpha</span>: CGFloat) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">stroke</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">blendMode</span>: CGBlendMode, <span class="pl-en" style="color:#795DA3;">alpha</span> <span class="pl-smi">alpha</span>: CGFloat) <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">copy</span>(<span class="pl-en" style="color:#795DA3;">with</span> <span class="pl-smi">zone</span>: NSZone <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-c1" style="color:#0086B3;">nil</span>) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">AnyObject</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">encode</span>(<span class="pl-en" style="color:#795DA3;">with</span> <span class="pl-smi">aCoder</span>: NSCoder)
}</pre>
													</div>
<p style="font-family:" font-size:16px;"="">
	可以看到，在Swift 3版本里，原来API里很多描述类型信息的部分都被去掉了。转换后的结果，更接近Swift API设计指南中的要求。现在，Swift开发者可以通过类似foo.copy()这样的方式，拷贝任何遵从NSCopying的对象，而不用再像原来foo.copyWithZone(nil)这样的方式。
														</p>
														<h2 style="font-size:1.75em;font-family:;">
															<a id="user-content-实现过程" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%AE%9E%E7%8E%B0%E8%BF%87%E7%A8%8B"></a>实现过程
														</h2>
<p style="font-family:" font-size:16px;"="">
	这份提议的一个试验性实现在Swift main repository中。Swift编译器提供了一些开关帮助我们查看按照这份提议中的描述，被引入的Objective-C API以及Swift代码自身的转换结果（例如，通过utils/omit-needless-words.py脚本）。这些开关是：
													</p>
<ul style="font-family:" font-size:16px;"="">
													<li>
														--enable-omit-needless-words：这个开关启用了对Clang importer绝大多数的改动（上一节中提到的1，2，4，5）。它主要适合用来打印在Master和<a href="https://github.com/apple/swift/tree/swift-2.2-branch">Swift 2.2</a>分支上，Swift对Objective-C模块提供的接口。在<a href="https://github.com/apple/swift/tree/swift-3-api-guidelines">Swift 3 API Guidelines分支</a>上，它默认是开启的；
													</li>
													<li>
														--enable-infer-default-arguments：这个开关启用了Clang importer中，对参数默认值的干涉（上一节的3）；
													</li>
													<li>
														--swift-migration：仅在<a href="https://github.com/apple/swift/tree/swift-2.2-branch">Swift 2.2分支</a>上才有的开关，这个选项通过添加"Fix-Its"，执行把名称从Swift 2迁移到Swift 3的基本转换。通过和其它编译器开关（例如：-fixit-code，-fixit-all）以及一个收集和应用“Fix-Its”的脚本（utils/apply-fixit-edits.py）一起使用，这个开关为我们提供的基础迁移工作可以帮助我们了解Swift代码在各种声明和调用场景里，按照这份提议被转换后的样子；
													</li>
														</ul>
<p style="font-family:" font-size:16px;"="">
	为了使用转换后的名称，真正编译Swift 3代码，可以使用<a href="https://github.com/apple/swift/tree/swift-3-api-guidelines">Swift 3 API Guidelines</a>分支。编译器默认启用了上述功能并带有随之一起改动的标准库。
															</p>
															<h2 style="font-size:1.75em;font-family:;">
																<a id="user-content-设计细节" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E8%AE%BE%E8%AE%A1%E7%BB%86%E8%8A%82"></a>设计细节
															</h2>
<p style="font-family:" font-size:16px;"="">
	这部分描述了上述改变规则中第2-5条的试验性实现细节。真正的实现在Swift代码树中，主要是<a href="https://github.com/apple/swift/blob/master/lib/Basic/StringExtras.cpp">lib/Basic/StringExtras.cpp</a>中的omitNeedlessWords函数。
														</p>
<p style="font-family:" font-size:16px;"="">
	接下来的描述，和参与名称翻译的Objective-C API紧密相关。例如：startWithQueue:compeltionHandler:是个有两个selector片段的方法，startWithQueue和completionHandler。这个selector翻译成Swift版本应该是startWithQueue(_:completionHandler:)。
															</p>
															<h3 style="font-size:1.5em;font-family:;">
																<a id="user-content-去掉冗余的名称" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%8E%BB%E6%8E%89%E5%86%97%E4%BD%99%E7%9A%84%E5%90%8D%E7%A7%B0"></a>去掉冗余的名称
															</h3>
<p style="font-family:" font-size:16px;"="">
	Objective-C API中经常会包含参数或返回值的类型名称，它们应该在Swift API中统统被去掉。下面的这些规则，用于识别并且去掉这些表示类型名称的单词。[[详见API设计原则之忽略不需要的单词]]。
														</p>
														<h4 style="font-size:1.25em;font-family:;">
															<a id="user-content-定义类型名称" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B%E5%90%8D%E7%A7%B0"></a>定义类型名称
														</h4>
<p style="font-family:" font-size:16px;"="">
	匹配的过程是在老版本Swift API的selector片段里，搜索特定的<span style="font-weight:bolder;">类型名称</span>后缀，这些类型名称定义如下：
															</p>
<ul style="font-family:" font-size:16px;"="">
															<li>
																对于绝大部分Objective-C类型来说，类型名称就是忽略nullable之后，Swift引入的名称，例如：
															</li>
																</ul>
<table style="width:888px;color:#333333;font-family:" font-size:16px;"="">
																<tbody>
																	<tr>
																		<th style="border:1px solid #DDDDDD;">
																			Objective-C type
																		</th>
																		<th style="border:1px solid #DDDDDD;">
																			Type name
																		</th>
																	</tr>
																</tbody>
																<tbody>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			float
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			Float
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			nullable NSString
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			String
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			UIDocument
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			UIDocument
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			nullable UIDocument
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			UIDocument
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			NSInteger
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			NSInteger
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			UIUInteger
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			NSUInteger
																		</td>
																	</tr>
																	<tr>
																		<td style="border:1px solid #DDDDDD;">
																			CGFloat
																		</td>
																		<td style="border:1px solid #DDDDDD;">
																			CGFloat
																		</td>
																	</tr>
																</tbody>
																	</table>
<ul style="font-family:" font-size:16px;"="">
																	<li>
																		当Objective-C类型是一个block时，Swift中的类型名称是Block；
																	</li>
																	<li>
																		当Objective-C的类型是一个函数指针或引用时，Swift中的类型名称是Function；
																	</li>
																	<li>
																		当Objective-C的类型是一个除NSInteger、NSUInteger或CGFloat之外的typedef时，Swift中的类型名称则是这些typedef实际代表的类型的名称。例如，Objective-C中的类型是UILayoutPriority，实际上它是一个float类型的typedef，我们将（在老版本的Swift API里）尝试匹配字符串Float。[详见API设计原则之为弱类型提供信息补偿]。
																	</li>
																		</ul>
																		<h4 style="font-size:1.25em;font-family:;">
																			<a id="user-content-匹配类型名称" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%8C%B9%E9%85%8D%E7%B1%BB%E5%9E%8B%E5%90%8D%E7%A7%B0"></a>匹配类型名称
																		</h4>
<p style="font-family:" font-size:16px;"="">
	为了在selector片段中删除冗余的类型信息，我们需要在selector中匹配包含上述类型信息的字符串。
																			</p>
<p style="font-family:" font-size:16px;"="">
	全部匹配都是由以下两个基本规则来管理的：
																		</p>
<ul style="font-family:" font-size:16px;"="">
																		<li>
																			<span style="font-weight:bolder;">在单词开始和结束的边界进行匹配</span>：无论是selector片段内部，还是类型名称，单词边界位置都是一个字符串的开始或结束，以及每一个大写字母前面。把每一个大写字母作为单词边界可以让我们匹配到“大写字母缩略词”，而无需单独维护一个特殊的缩略词或前缀列表；
																		</li>
																	</ul>
<p style="font-family:" font-size:16px;"="">
	例如，下面的URL是匹配的：
																		</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">documentForURL</span>(_: NSURL) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSDocument?</pre>
																			</div>
<p style="font-family:" font-size:16px;"="">
	但是，下面的View，由于在单词中间，它是不能被匹配的。
																				</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">var</span> thumbnailPreview <span class="pl-k" style="color:#A71D5D;">:</span> UIView <span class="pl-c" style="color:#969896;">// not matched</span></pre>
																					</div>
<ul style="font-family:" font-size:16px;"="">
																					<li>
																						<span style="font-weight:bolder;">匹配的字符扩展到类型名称的结尾</span>：因为我们支持匹配一个类型名称的后缀，因此：
																					</li>
																						</ul>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">constraintEqualToAnchor</span>(anchor: NSLayoutAnchor) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSLayoutConstraint?</pre>
																							</div>
<p style="font-family:" font-size:16px;"="">
	可以被简化成：
																								</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">constraintEqualTo</span>(anchor: NSLayoutAnchor) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSLayoutConstraint?</pre>
																									</div>
<p style="font-family:" font-size:16px;"="">
	基于以上两个原则，我们可以执行以下一系列的匹配过程：
																										</p>
<ul style="font-family:" font-size:16px;"="">
																										<li>
																											<p>
																												<span style="font-weight:bolder;">基本匹配</span>：
																											</p>
																											<ul>
																												<li>
																													selector片段中的字串匹配类型名称的字串，例如：appendString中的String匹配NSString中的String：func appendString(_: NSString)；
																												</li>
																												<li>
																													Selector片段中的Index匹配类型名称中的Int，例如：func characterAtIndex(_: Int) -&gt; unichar；
																												</li>
																											</ul>
																										</li>
																										<li>
																											<p>
																												<span style="font-weight:bolder;">集合相关匹配（Collection matches）</span>：
																											</p>
																											<ul>
																												<li>
																													Selector片段中的Indexes或Indices匹配类型名称中的IndexSet，例如：func removeObjectsAtIndexes(_: NSIndexSet)；
																												</li>
																												<li>
																													如果selector片段中的复数名词的单数形式匹配集合中元素的类型，那么selector中的复数名词匹配集合类型名称，例如：func arrangeObjects(_: [AnyObject]) -&gt; [AnyObject]
																												</li>
																											</ul>
																										</li>
																											</ul>
<p style="font-family:" font-size:16px;"=""> <span style="font-weight:bolder;">特殊后缀匹配</span>：
																												</p>
<ul style="font-family:" font-size:16px;"="">
																												<li>
																													在selector片段中，用一个空字符串后缀匹配类型名称中的Type或_t，例如：
																												</li>
																													</ul>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-c" style="color:#969896;">// 注：用一个空字符串匹配到了Type，因此SaveOperation加上</span> <span class="pl-c" style="color:#969896;">// 空字符串就匹配了SaveOperationType，</span> <span class="pl-c" style="color:#969896;">// 于是Selector中的SaveOperation的部分就可以删除了。</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">writableTypesForSaveOperation</span>(
    _: NSSaveOperationType) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> [<span class="pl-c1" style="color:#0086B3;">String</span>] <span class="pl-c" style="color:#969896;">// 注：用一个空字符串匹配到了Type。</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">objectForKey</span>(_: KeyType) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">AnyObject</span> <span class="pl-c" style="color:#969896;">// 注：用一个空字符串匹配到了_t。</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">startWithQueue</span>(_: dispatch_queue_t, 
    completionHandler: MKMapSnapshotCompletionhandler)</pre>
																														</div>
<pre>* selector片段中的空字符串可以匹配类型名称中“数字+D”形式的后缀，例如：</pre>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-c" style="color:#969896;">// Coordinate+空字符串匹配到了2D</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">pointForCoordinate</span>(_: CLLocationCoordinate2D) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSPoint</pre>
																													</div>
																													<h4 style="font-size:1.25em;font-family:;">
																														<a id="user-content-删除类型名称时的限制" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%88%A0%E9%99%A4%E7%B1%BB%E5%9E%8B%E5%90%8D%E7%A7%B0%E6%97%B6%E7%9A%84%E9%99%90%E5%88%B6"></a>删除类型名称时的限制
																													</h4>
<p style="font-family:" font-size:16px;"="">
	如果在删除selector名称时违背了以下任何限制，那么删除行为将不会进行：
																														</p>
<ul style="font-family:" font-size:16px;"="">
																														<li>
																															<span style="font-weight:bolder;">不要删光所有的selector</span>；
																														</li>
																														<li>
																															<span style="font-weight:bolder;">不要把selector的第一个片段转换成Swift关键字</span>：
																														</li>
																															</ul>
<p style="font-family:" font-size:16px;"="">
	在Swift里，Objective-C方法中的第一个selector片段会变成<span style="font-weight:bolder;">构建一个方法名称的基础</span>或者<span style="font-weight:bolder;">一个属性的名字</span>。
																																</p>
<p style="font-family:" font-size:16px;"="">
	它们之中的任何一种，都不能是Swift关键字，否则，用户就需要使用反引号来使用它们。
																															</p>
<p style="font-family:" font-size:16px;"="">
	例如，下面的用法是合理的：
																																</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSParagraphStyle { <span class="pl-k" style="color:#A71D5D;">class</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">defaultParagraphStyle</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSParagraphStyle
} <span class="pl-k" style="color:#A71D5D;">let</span> defaultStyle <span class="pl-k" style="color:#A71D5D;">=</span> NSParagraphStyle<span class="pl-k" style="color:#A71D5D;">.</span>defaultParagraphStyle() <span class="pl-c" style="color:#969896;">// OK</span></pre>
																																	</div>
<p style="font-family:" font-size:16px;"="">
	如果我们删掉ParagraphStyle，用起来就会很糟糕：
																																		</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSParagraphStyle {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">class</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">`default`</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSParagraphStyle
} <span class="pl-k" style="color:#A71D5D;">let</span> defaultStyle <span class="pl-k" style="color:#A71D5D;">=</span> NSParagraphStyle<span class="pl-k" style="color:#A71D5D;">.</span>`<span class="pl-k" style="color:#A71D5D;">default</span>`() <span class="pl-c" style="color:#969896;">// Awkward</span></pre>
																																			</div>
<p style="font-family:" font-size:16px;"="">
	Objective-C方法名中的其它selector片段，会变成Swift方法的参数label，这个label是允许使用Swift关键字的，例如：
																																				</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre>receiver<span class="pl-k" style="color:#A71D5D;">.</span>handle(someMessage, <span class="pl-k" style="color:#A71D5D;">for</span>: somebody) <span class="pl-c" style="color:#969896;">// OK</span></pre>
																																					</div>
<ul style="font-family:" font-size:16px;"="">
																																					<li>
																																						<span style="font-weight:bolder;">不要把方法名称转换成“get”，“Set”，“with”, “for”或“using”</span>，用这些单词形成的方法名称表意非常空洞；
																																					</li>
																																					<li>
																																						<span style="font-weight:bolder;">不要删除方法名称中那些介绍参数的后缀，除非这个后缀前面直接连接一个介词、动词或动名词</span>：
																																					</li>
																																						</ul>
<p style="font-family:" font-size:16px;"="">
	这种启发式转换帮助我们避免破坏那些介绍参数的名词短语。直接删掉名词短语后缀通常不会带来我们预期的表意结果。例如：
																																							</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">setTextColor</span>(_: UIColor) <span class="pl-k" style="color:#A71D5D;">...</span> button<span class="pl-k" style="color:#A71D5D;">.</span>setTextColor(<span class="pl-k" style="color:#A71D5D;">.</span>red()) <span class="pl-c" style="color:#969896;">// clear</span></pre>
																																								</div>
<p style="font-family:" font-size:16px;"="">
	如果我们删掉方法名中的Color，只剩下Text，调用方法时表达的语意就会让人困惑：
																																									</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">setText</span>(_: UIColor) <span class="pl-k" style="color:#A71D5D;">...</span> button<span class="pl-k" style="color:#A71D5D;">.</span>setText(<span class="pl-k" style="color:#A71D5D;">.</span>red()) <span class="pl-c" style="color:#969896;">// appears to be setting the text!</span></pre>
																																										</div>
<ul style="font-family:" font-size:16px;"="">
																																										<li>
																																											<span style="font-weight:bolder;">如果方法的名字匹配它所在的类型中的一个属性，不要转换方法名</span>：
																																										</li>
																																											</ul>
<p style="font-family:" font-size:16px;"="">
	这种启发式转换使我们可以避免让那些修改类属性的方法转换出过于泛泛的名字，例如：
																																												</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">var</span> gestureRecognizers: [UIGestureRecognizer] <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">addGestureRecognizer</span>(_: UIGestureRecognizer)</pre>
																																													</div>
<p style="font-family:" font-size:16px;"="">
	如果我们删掉方法中的GestureRecognizer，只留下add，对于一个实际上是在修改gesturerecognizers属性的方法来说，这个名字明显太泛泛了。
																																														</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">var</span> gestureRecognizers: [UIGestureRecognizer] <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">add</span>(_: UIGestureRecognizer) // should indicate that we're adding to the property</pre>
																																															</div>
																																															<h4 style="font-size:1.25em;font-family:;">
																																																<a id="user-content-删除类型名称的步骤" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%88%A0%E9%99%A4%E7%B1%BB%E5%9E%8B%E5%90%8D%E7%A7%B0%E7%9A%84%E6%AD%A5%E9%AA%A4"></a>删除类型名称的步骤
																																															</h4>
<p style="font-family:" font-size:16px;"="">
	我们按照下面的步骤删除冗余的名字：
																																																</p>
<ol style="font-family:" font-size:16px;"="">
																																																<li>
																																																	<p>
																																																		<span style="font-weight:bolder;">删除头部的结果类型信息</span>。特别是，当以下情形的时候：
																																																	</p>
																																																	<ul>
																																																		<li>
																																																			当方法返回一个自身所在的类型时；
																																																		</li>
																																																		<li>
																																																			并且这个类型的名称匹配方法中第一个selector片段；
																																																		</li>
																																																		<li>
																																																			匹配到的名词后面，紧跟一个介词；
																																																		</li>
																																																	</ul>
																																																	<p>
																																																		就可以删除掉这个匹配。
																																																	</p>
																																																	<p>
																																																		通常，匹配以上这些条件的属性和方法，它们都用于把自身类型变成其它某种等价形式的值。
																																																	</p>
																																																	<p>
																																																		例如：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSColor {
&nbsp;&nbsp;<span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">colorWithAlphaComponent</span>(_: CGFloat) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSColor
} <span class="pl-k" style="color:#A71D5D;">let</span> translucentForeground <span class="pl-k" style="color:#A71D5D;">=</span> foregroundColor<span class="pl-k" style="color:#A71D5D;">.</span>colorWithAlphaComponent(<span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">5</span>)</pre>
																																																	</div>
																																																	<p>
																																																		可以被简化成：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSColor { <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withAlphaComponent</span>(_: CGFloat) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSColor
} <span class="pl-k" style="color:#A71D5D;">let</span> translucentForeground <span class="pl-k" style="color:#A71D5D;">=</span> foregroundColor<span class="pl-k" style="color:#A71D5D;">.</span>withAlphaComponent(<span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">5</span>)</pre>
																																																	</div>
																																																</li>
																																																<li>
																																																	<p>
																																																		<span style="font-weight:bolder;">删掉多余的介词By</span>。特别是，当以下情形的时候：
																																																	</p>
																																																	<ul>
																																																		<li>
																																																			在第一步中删掉了开始的名词之后；
																																																		</li>
																																																		<li>
																																																			方法名称中，剩余的部分用By+动名词的形式；
																																																		</li>
																																																	</ul>
																																																	<p>
																																																		就可以删掉多余的By了。
																																																	</p>
																																																	<p>
																																																		这种启发式方法可以让我们使用类似a = b.frobnicating(c)的方法调用形式。例如：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSString { <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">stringByApplyingTransform</span>(_: NSString, 
        reverse: <span class="pl-c1" style="color:#0086B3;">Bool</span>) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSString?
} <span class="pl-k" style="color:#A71D5D;">let</span> sanitizedInput <span class="pl-k" style="color:#A71D5D;">=</span> rawInput<span class="pl-k" style="color:#A71D5D;">.</span>stringByApplyingTransform(
        NSStringTransformToXMLHex, reverse: <span class="pl-c1" style="color:#0086B3;">false</span>)</pre>
																																																	</div>
																																																	<p>
																																																		就可以通过第一步和第二步，被简化成：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSString {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">applyingTransform</span>(
&nbsp;&nbsp;      _: NSString, reverse: <span class="pl-c1" style="color:#0086B3;">Bool</span> &nbsp;&nbsp;  ) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NString?
} <span class="pl-k" style="color:#A71D5D;">let</span> sanitizedInput <span class="pl-k" style="color:#A71D5D;">=</span> rawInput<span class="pl-k" style="color:#A71D5D;">.</span>applyingTransform(NSStringTransformToXMLHex, 
        reverse: <span class="pl-c1" style="color:#0086B3;">false</span>)</pre>
																																																	</div>
																																																</li>
																																																<li>
																																																	<p>
																																																		<span style="font-weight:bolder;">在方法签名的最后一个selector片段中，删除任何匹配到的类型名称</span>。特别是以下类型：
																																																	</p>
																																																	<table style="width:856px;">
																																																		<tbody>
																																																			<tr>
																																																				<th style="border:1px solid #DDDDDD;">
																																																					方法名称的尾部是
																																																				</th>
																																																				<th style="border:1px solid #DDDDDD;">
																																																					删除匹配到的
																																																				</th>
																																																			</tr>
																																																		</tbody>
																																																		<tbody>
																																																			<tr>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					用于介绍参数的selector片段
																																																				</td>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					参数类型名称
																																																				</td>
																																																			</tr>
																																																			<tr>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					一个属性名称
																																																				</td>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					属性的类型名称
																																																				</td>
																																																			</tr>
																																																			<tr>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					一个不带参数的方法
																																																				</td>
																																																				<td style="border:1px solid #DDDDDD;">
																																																					返回值的类型名称
																																																				</td>
																																																			</tr>
																																																		</tbody>
																																																	</table>
																																																	<p>
																																																		例如，下面这些情况：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSDocumentController {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">documentForURL</span>(
&nbsp;&nbsp; <span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">url</span>: NSURL) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSDocument? // parameter introducer
} <span class="pl-k" style="color:#A71D5D;">extension</span> NSManagedObjectContext {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">var</span> parentContext: NSManagedObjectContext? <span class="pl-c" style="color:#969896;">// property</span> } <span class="pl-k" style="color:#A71D5D;">extension</span> UIColor {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">class</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">darkGrayColor</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIColor  // zero-argument method
} <span class="pl-k" style="color:#A71D5D;">...</span> myDocument <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>documentForURL(locationOfFile) <span class="pl-k" style="color:#A71D5D;">if</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>managedObjectContext<span class="pl-k" style="color:#A71D5D;">.</span>parentContext <span class="pl-k" style="color:#A71D5D;">!=</span> changedContext { <span class="pl-k" style="color:#A71D5D;">return</span> }

foregroundColor <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-k" style="color:#A71D5D;">.</span>darkGrayColor()</pre>
																																																	</div>
																																																	<p>
																																																		可以被简化成：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSDocumentController {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">documentFor</span>(<span class="pl-en" style="color:#795DA3;">_</span> <span class="pl-smi">url</span>: NSURL) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSDocument?
} <span class="pl-k" style="color:#A71D5D;">extension</span> NSManagedObjectContext {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">var</span> parent <span class="pl-k" style="color:#A71D5D;">:</span> NSManagedObjectContext?
} <span class="pl-k" style="color:#A71D5D;">extension</span> UIColor {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">class</span> <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">darkGray</span>() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIColor
} <span class="pl-k" style="color:#A71D5D;">...</span> myDocument <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>documentFor(locationOfFile) <span class="pl-k" style="color:#A71D5D;">if</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>managedObjectContext<span class="pl-k" style="color:#A71D5D;">.</span>parent <span class="pl-k" style="color:#A71D5D;">!=</span> changedContext { <span class="pl-k" style="color:#A71D5D;">return</span> }
foregroundColor <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-k" style="color:#A71D5D;">.</span>darkGray()</pre>
																																																	</div>
																																																</li>
																																																<li>
																																																	<p>
																																																		<span style="font-weight:bolder;">只要匹配到的类型名称前面直接连接动词，即便这个类型名称在方法名中间也可以删掉它</span>，例如：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> UIViewController {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">dismissViewControllerAnimated</span>(
&nbsp;&nbsp;      flag: <span class="pl-c1" style="color:#0086B3;">Bool</span>, 
&nbsp;&nbsp;      completion: (() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Void</span>)? = nil)
}</pre>
																																																	</div>
																																																	<p>
																																																		可以被简化成：
																																																	</p>
																																																	<div class="highlight highlight-source-swift">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> UIViewController {
&nbsp;&nbsp; <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">dismissAnimated</span>(
&nbsp;&nbsp;      flag: <span class="pl-c1" style="color:#0086B3;">Bool</span>, 
&nbsp;&nbsp;      completion: (() <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Void</span>)? = nil)
}</pre>
																																																	</div>
																																																</li>
																																																	</ol>
																																																	<h5 style="font-size:1em;font-family:;">
																																																		<a id="user-content-为什么删除一定要按照顺序执行呢" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E4%B8%BA%E4%BB%80%E4%B9%88%E5%88%A0%E9%99%A4%E4%B8%80%E5%AE%9A%E8%A6%81%E6%8C%89%E7%85%A7%E9%A1%BA%E5%BA%8F%E6%89%A7%E8%A1%8C%E5%91%A2"></a>为什么删除一定要按照顺序执行呢？
																																																	</h5>
<p style="font-family:" font-size:16px;"="">
	下面的简化过程有些在方法名称的第一个selector片段中匹配，有些在方法名称的结尾匹配。当<a href="https://github.com/Boxue/swift-api-design-guidelines/blob/master">名称删除限制</a>阻止我们从开始和结尾进行删除时，从头部开始删除名称可以保持方法家族的一致性，例如，对于NSFontDescriptor来说：
																																																		</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fontDescriptorWithSymbolicTraits</span>(
    _: NSFontSymbolicTraits) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSFontDescriptor <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fontDescriptorWithSize</span>(
    _: CGFloat) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fontDescriptorWithMatrix</span>(
    _: CGAffineTransform) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">...</span></pre>
																																																			</div>
<p style="font-family:" font-size:16px;"="">
	按照头部匹配规则，它们可以变成这样：
																																																				</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withSymbolicTraits</span>(
    _: UIFontDescriptorSymbolicTraits) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withSize</span>(
    _: CGFloat) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withMatrix</span>(
    _: CGAffineTransform) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">...</span></pre>
																																																					</div>
<p style="font-family:" font-size:16px;"="">
	如果我们坚持从尾部进行删除，删掉第一个方法中的SymbolicTraits之后，我们就无法再继续删除头部的fontDescriptor了，因为这违背了我们在<a href="https://github.com/Boxue/swift-api-design-guidelines/blob/master">名称限制约束</a>中定义的原则，只留下一个with表意是不够的。
																																																						</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">fontDescriptorWith</span>(
    _: NSFontSymbolicTraits) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSFontDescriptor // inconsistent <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withSize</span>(_: CGFloat) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">withMatrix</span>(_: CGAffineTransform) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> UIFontDescriptor <span class="pl-k" style="color:#A71D5D;">...</span></pre>
																																																							</div>
<blockquote style="color:#777777;font-family:" font-size:16px;"="">
																																																							<p>
																																																								注：这样一来，我们就破坏了原来家族方法的名称一致性。
																																																							</p>
																																																								</blockquote>
																																																								<h4 style="font-size:1.25em;font-family:;">
																																																									<a id="user-content-添加参数默认值" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E6%B7%BB%E5%8A%A0%E5%8F%82%E6%95%B0%E9%BB%98%E8%AE%A4%E5%80%BC"></a>添加参数默认值
																																																								</h4>
<p style="font-family:" font-size:16px;"="">
	出了那些只有一个参数的setter方法之外，在以下情况时，应该给参数添加默认值：
																																																									</p>
<ul style="font-family:" font-size:16px;"="">
																																																									<li>
																																																										让<span style="font-weight:bolder;">可以为空的trailing closure参数</span>值为nil；
																																																									</li>
																																																									<li>
																																																										让<span style="font-weight:bolder;">可以为空的NSZone参数</span>默认为nil。Zones几乎不在Swift中使用，它们应该总是为nil的；
																																																									</li>
																																																									<li>
																																																										让<span style="font-weight:bolder;">参数名中带有"Options"字样的参数</span>值默认为[]，表示空的选项集合；
																																																									</li>
																																																									<li>
																																																										和选项、属性或信息有关的NSDictionary参数，默认值为[:]；
																																																									</li>
																																																										</ul>
<p style="font-family:" font-size:16px;"="">
	把这些规则集合起来，这个启发式方法可以把下面的代码：
																																																											</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre>rootViewController<span class="pl-k" style="color:#A71D5D;">.</span>presentViewController(
     alert, animated: <span class="pl-c1" style="color:#0086B3;">true</span>, completion: <span class="pl-c1" style="color:#0086B3;">nil</span>)

UIView<span class="pl-k" style="color:#A71D5D;">.</span>animateWithDuration(<span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">2</span>, delay: <span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">0</span>, 
    options: [], 
    animations: { <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>logo<span class="pl-k" style="color:#A71D5D;">.</span>alpha <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">0</span> }) {
    _ <span class="pl-k" style="color:#A71D5D;">in</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>logo<span class="pl-k" style="color:#A71D5D;">.</span>hidden <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-c1" style="color:#0086B3;">true</span> }</pre>
																																																												</div>
<p style="font-family:" font-size:16px;"="">
	变成这个样子：
																																																													</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre>rootViewController<span class="pl-k" style="color:#A71D5D;">.</span>present(alert, animated: <span class="pl-c1" style="color:#0086B3;">true</span>)

UIView<span class="pl-k" style="color:#A71D5D;">.</span>animateWithDuration(<span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">2</span>, delay: <span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">0</span>, 
    animations: { <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>logo<span class="pl-k" style="color:#A71D5D;">.</span>alpha <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-c1" style="color:#0086B3;">0</span><span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">0</span> }) { 
    _ <span class="pl-k" style="color:#A71D5D;">in</span> <span class="pl-k" style="color:#A71D5D;">self</span><span class="pl-k" style="color:#A71D5D;">.</span>logo<span class="pl-k" style="color:#A71D5D;">.</span>hidden <span class="pl-k" style="color:#A71D5D;">=</span> <span class="pl-c1" style="color:#0086B3;">true</span> }
</pre>
																																																														</div>
																																																														<h4 style="font-size:1.25em;font-family:;">
																																																															<a id="user-content-为第一个参数添加label" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E4%B8%BA%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%E6%B7%BB%E5%8A%A0label"></a>为第一个参数添加label
																																																														</h4>
<p style="font-family:" font-size:16px;"="">
	如果方法名称中第一个selector片段中包含一个介词，<span style="font-weight:bolder;">就把这个selector片段从最后一个介词处分开</span>，把这个selector中，这个介词后面的部分变成第一个参数的label。
																																																															</p>
<p style="font-family:" font-size:16px;"="">
	除了为大量API的第一个参数添加label之外，当第一个参数有默认值时，这种启发式方法还能消除方法被调用时，方法名称表达的模糊语义。例如：
																																																														</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> UIBezierPath { <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">enumerateObjectsWith</span>(
        _: NSEnumerationOptions <span class="pl-k" style="color:#A71D5D;">=</span> [], 
        using: (<span class="pl-c1" style="color:#0086B3;">AnyObject</span>, <span class="pl-c1" style="color:#0086B3;">UnsafeMutablePointer</span>) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Void</span>)
}

array<span class="pl-k" style="color:#A71D5D;">.</span>enumerateObjectsWith(<span class="pl-k" style="color:#A71D5D;">.</span>Reverse) { <span class="pl-c" style="color:#969896;">// OK</span> <span class="pl-c" style="color:#969896;">// ..</span> }

array<span class="pl-k" style="color:#A71D5D;">.</span>enumerateObjectsWith() { <span class="pl-c" style="color:#969896;">// ?? With what?</span> <span class="pl-c" style="color:#969896;">// ..</span> }</pre>
																																																													</div>
<p style="font-family:" font-size:16px;"="">
	变成：
																																																												</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSArray { <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">enumerateObjects</span>( <span class="pl-en" style="color:#795DA3;">options</span> <span class="pl-smi">_</span>: NSEnumerationOptions <span class="pl-k" style="color:#A71D5D;">=</span> [], 
      using: (<span class="pl-c1" style="color:#0086B3;">AnyObject</span>, <span class="pl-c1" style="color:#0086B3;">UnsafeMutablePointer</span>) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> <span class="pl-c1" style="color:#0086B3;">Void</span>)
}

array<span class="pl-k" style="color:#A71D5D;">.</span>enumerateObjects(options: <span class="pl-k" style="color:#A71D5D;">.</span>Reverse) { <span class="pl-c" style="color:#969896;">// OK</span> <span class="pl-c" style="color:#969896;">// ..</span> }

array<span class="pl-k" style="color:#A71D5D;">.</span>enumerateObjects() { <span class="pl-c" style="color:#969896;">// OK</span> <span class="pl-c" style="color:#969896;">// ..</span> }</pre>
																																																											</div>
																																																											<h4 style="font-size:1.25em;font-family:;">
																																																												<a id="user-content-为bool语义的属性添加is前缀" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E4%B8%BAbool%E8%AF%AD%E4%B9%89%E7%9A%84%E5%B1%9E%E6%80%A7%E6%B7%BB%E5%8A%A0is%E5%89%8D%E7%BC%80"></a>为Bool语义的属性添加“is”前缀
																																																											</h4>
<p style="font-family:" font-size:16px;"="">
	在Objective-C里，表达Bool语义的属性，使用对应的getter方法作为这个属性在Swift中的名字。例如：
																																																										</p>
<div class="highlight highlight-source-objc" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">@interface</span> <span class="pl-en" style="color:#795DA3;">NSBezierPath</span> : <span class="pl-e" style="color:#795DA3;">NSObject</span> <span class="pl-k" style="color:#A71D5D;">@property</span> (<span class="pl-k" style="color:#A71D5D;">readonly</span>,<span class="pl-k" style="color:#A71D5D;">getter</span>=isEmpty) <span class="pl-k" style="color:#A71D5D;">BOOL</span> empty;</pre>
																																																											</div>
<p style="font-family:" font-size:16px;"="">
	会变成：
																																																												</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">extension</span> NSBezierPath { <span class="pl-k" style="color:#A71D5D;">var</span> isEmpty: <span class="pl-c1" style="color:#0086B3;">Bool</span> } <span class="pl-k" style="color:#A71D5D;">if</span> path<span class="pl-k" style="color:#A71D5D;">.</span><span class="pl-c1" style="color:#0086B3;">isEmpty</span> { <span class="pl-k" style="color:#A71D5D;">...</span> }</pre>
																																																													</div>
																																																													<h3 style="font-size:1.5em;font-family:;">
																																																														<a id="user-content-实现比较方法时遵从的准则" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%AE%9E%E7%8E%B0%E6%AF%94%E8%BE%83%E6%96%B9%E6%B3%95%E6%97%B6%E9%81%B5%E4%BB%8E%E7%9A%84%E5%87%86%E5%88%99"></a>实现比较方法时遵从的准则
																																																													</h3>
<p style="font-family:" font-size:16px;"="">
	现如今，为了实现对象比较，例如对NSDate来说，开发者经常会扩展NSDate让它遵从Comparable或使用NSDate的compare(_:) -&gt; NSComparisonResult方法。在这些场景里，对NSDate使用表操作符会有效提高代码可读性，例如someDate &lt; today要比someDate.comare(today) == .OrderedAscending要清晰的多。由于转换过程可以确定一个类是否实现了Objective-C中的比较方法，所有实现了这个方法的类都可以按照遵从Comparable&nbsp;protocol的方式引入。
																																																														</p>
<p style="font-family:" font-size:16px;"="">
	不仅仅是NSDate，Foundation中的一些其它类也会被这个改变影响，例如：
																																																													</p>
<div class="highlight highlight-source-swift" style="font-family:" font-size:16px;"="">
<pre><span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">compare</span>(other: NSDate) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSComparisonResult <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">compare</span>(decimalNumber: NSNumber) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSComparisonResult <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">compare</span>(otherObject: NSIndexPath) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSComparisonResult <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">compare</span>(string: <span class="pl-c1" style="color:#0086B3;">String</span>) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSComparisonResult <span class="pl-k" style="color:#A71D5D;">func</span> <span class="pl-en" style="color:#795DA3;">compare</span>(otherNumber: NSNumber) <span class="pl-k" style="color:#A71D5D;">-&gt;</span> NSComparisonResult</pre>
																																																												</div>
																																																												<h3 style="font-size:1.5em;font-family:;">
																																																													<a id="user-content-对已有代码的影响" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%AF%B9%E5%B7%B2%E6%9C%89%E4%BB%A3%E7%A0%81%E7%9A%84%E5%BD%B1%E5%93%8D"></a>对已有代码的影响
																																																												</h3>
<p style="font-family:" font-size:16px;"="">
	这份提议中的改变为使用Objective-C框架的已有的Swift代码引入了大量破坏性改变（breaking change）。以至于，我们需要一个迁移工具把Swift 2代码迁移到Swift 3。在<a href="https://github.com/Boxue/swift-api-design-guidelines/blob/master">实现过程</a>中描述的-swift3-migration开关为这样的转换工具提供了基本信息。另外，编译器需要为那些引用了旧版本Objective-C名称的Swift代码提供良好的错误信息（带有修改建议），除此之外，还应该提供一个辅助的通过旧版本名称进行查询的机制。
																																																											</p>
																																																											<h3 style="font-size:1.5em;font-family:;">
																																																												<a id="user-content-声明" class="anchor" href="https://github.com/Boxue/swift-api-design-guidelines/blob/master/SE-0005%20better-translation-of-objective-c-api-into-swift.md#%E5%A3%B0%E6%98%8E"></a>声明
																																																											</h3>
<p style="font-family:" font-size:16px;"="">
	为了最终形成<a href="https://swift.org/documentation/api-design-guidelines">Swift API设计指南</a>，这份自动名称转换提议由Dmitri Hrybenko, Ted Kremenek, Chris Lattner, Alex Migicovsky, Max Moiseev, Ali Ozer和Tony Parker开发。
																																																												</p>
<p style="font-family:" font-size:16px;"="">
	补充添加进来的comparable部分之前由<a href="https://github.com/chrisamanse">Chris Amanse</a>提交到了<a href="https://swift.org/core-libraries/">core-libraries</a>邮件列表中。Philippe Hausler进行review之后，添加到了这份提议中。
																																																											</p> </p>
			     </div>

 		</div>
	</div>
  </div>
  
  
	<div class="site-footer container clearfix">   
	     <ul class="site-footer-links">
	       
	         <li><a href="http://www.swiftv.cn/news/page/lianxi"  >友情链接：</a></li>
	         <li><a href="http://www.swiftv.net"  target="_blank"  >Swift的那点事</a></li>
	         <li><a href="http://Letsswift.com/"  target="_blank"  >一起Swift</a></li>
	         <li><a href="http://www.ucai.cn/index.php?app=home&mod=Public&act=blogroll"  target="_blank"  >优才网</a></li>
	         <li><a href="http://www.9ria.com/"  target="_blank"  >9Ria游戏开发者社区</a></li>
	         <li><a href="http://www.swiftvip.cn"  >Swift中文网</a></li>
	         <li><a href="http://www.swift51.com/"  target="_blank"  >雨燕社区</a></li>
	         <li><a href="http://www.rupeng.com/friendsLink.shtml"  target="_blank"  >如鹏网(.NET/JAVA)</a></li>
	         <li><a href="http://www.ruanman.net"  target="_blank"  >软曼网</a></li>
	         <li><a href="https://mos.meituan.com/doc/about"  target="_blank"  >美团云</a></li>
	         <li><a href="http://www.maiziedu.com/"  target="_blank"  >麦子学院</a></li>
	         <li><a href="http://www.superqq.com/"  target="_blank"  >刚刚在线</a></li>
	         <li><a href="http://wgh.me/"  target="_blank"  >Sally</a></li>
	         <li><a href="http://www.oneapm.com/"  target="_blank"  >应用性能管理</a></li>
	         <li><a href="http://www.51ios.net/"  target="_blank"  >51iOS</a></li>
	         <li><a href="https://boxueio.com/"  target="_blank"  >泊学-全栈自学之路</a></li>
	         <li><a href="http://www.xsteach.com/"  target="_blank"  >邢帅教育</a></li>
	         <li><a href="http://www.i3done.com/"  target="_blank"  >3Done</a></li>
	         <li><a href="http://www.wyzc.com/"  target="_blank"  >我赢职场</a></li>
	         <li><a href="../page/qun.html"  >QQ群公布</a></li>
	         <li><a href="../page/yijian.html"  target="_blank"  >用户体验持续改进计划</a></li>
	     </ul>
	  
	  <div class="alert text-center"><a href="http://www.miitbeian.gov.cn/" target="_blank"> 粤ICP备14044760-1</a></div>
	</div>
	
	<div><script>window._bd_share_config={"common":{"bdSnsKey":{"tsina":"3474175605"},"bdText":"SwiftV课堂，是我见过的最有氛围的Swift视频学习站，体验很棒！小伙伴要不要一起来学习？限免哟~#SwiftV课堂#","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"3","bdPos":"left","bdTop":"214.5"}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script></div>



  
<div id="ScrollToTop" class="btnimg Button2" style="color: rgb(255, 255, 255);text-align: center; display: block; background-color: rgba(12, 173, 87, 0.69);padding: 11px; width: 60px;bottom:15px;">返回<br>顶部</div>
  <div id="login-modal" class="modal" data-url="/login/ajax"></div>
  <div id="modal" class="modal"></div>


<script src="http://static.vlms.cn/resources/js/jquery.form.min.js"></script>
<script src="http://static.vlms.cn/resources/js/bootstrapValidator.min.js"></script>
<script src="http://static.vlms.cn/resources/bootstrap/js/bootstrap.min.js"></script>
<script src="http://static.vlms.cn/resources/js/autocomplete.js"></script>
<script src="http://static.vlms.cn/resources/js/bootstrap-notify.js"></script>





  
  <!-- 统计代码 -->
  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?1e733c39d359b2149cd361e97999460b";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-52703035-1', 'auto');
  ga('send', 'pageview');
</script>


  </body>
</html>